﻿using Blm.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsMeasureItemRepository : BaseRepository<MsMeasureItem>, IMsMeasureItemRepository
    {
        /// <summary>
        /// 根据户型图Id获取测量项模板
        /// </summary>
        /// <param name="houseMapId"></param>
        /// <returns></returns>
        public async Task<List<MeasureItem>> GetMeasureItemTemplate(Guid houseMapId, int pointIndex)
        {
            var measureItems = await (
               from h in Db.MsMeasureItems
               join j in Db.MsMeasureTemplates on new { HouseMapId = h.HouseMapId, Id = h.Id } equals new { HouseMapId = j.HouseMapId, Id = j.Id }
               join p in Db.MsHouseMapPoints on new { HouseMapId = h.HouseMapId, PointId = h.PointId } equals new { HouseMapId = p.HouseMapId, PointId = p.PointId }
               where !j.IsDeleted && h.HouseMapId == houseMapId && p.PointIndex == pointIndex
               select new MeasureItem()
               {
                   HouseMapId = h.HouseMapId.ToString(),
                   PointId = h.PointId.ToString(),
                   Title = j.Title,
                   Id = j.Id,
                   Code = j.Code,
                   Desc = string.Empty,
                   DesignValues = j.DesignValues.ToObject<List<DesignValue>>()
               }).AsNoTracking().Distinct().ToListAsync().ConfigureAwait(false);
            return measureItems;
        }

        /// <summary>
        /// 根据户型图Id获取测量点信息
        /// </summary>
        /// <param name="houseMapId">户型图Id</param>
        /// <returns></returns>
        public async Task<List<MsMeasureItem>> Get(Guid houseMapId)
        {
            var measureItems = await (
                          from h in Db.MsMeasureItems
                          where h.HouseMapId == houseMapId
                          select h
                         ).AsNoTracking().ToListAsync().ConfigureAwait(false);
            return measureItems;
        }

        public async Task<List<MsMeasureItem>> GetDataByhouseMapIds(Guid[] houseMapIds)
        {
            var measureItems = await (
                          from h in Db.MsMeasureItems
                          where houseMapIds.Contains(h.HouseMapId)
                          select h
                         ).AsNoTracking().ToListAsync().ConfigureAwait(false);
            return measureItems;
        }
    }
}